<template>
  <div class="md-dialog-overlay" v-if="modelValue" @click.self="close">
    <div class="md-dialog">
      <div class="md-dialog__header">
        <h3 class="md-dialog__title">插入链接</h3>
        <button class="md-dialog__close" @click="close">
          <span>&times;</span>
        </button>
      </div>
      <div class="md-dialog__body">
        <div class="md-dialog__form-group">
          <label for="link-text">链接文本</label>
          <input
            id="link-text"
            type="text"
            v-model="linkText"
            placeholder="链接显示的文本"
            class="md-dialog__input"
            ref="textInputRef"
          />
        </div>
        <div class="md-dialog__form-group">
          <label for="link-url">链接地址</label>
          <input
            id="link-url"
            type="text"
            v-model="linkUrl"
            placeholder="https://example.com"
            class="md-dialog__input"
          />
        </div>
        <div class="md-dialog__form-group">
          <label for="link-title">链接标题 (可选)</label>
          <input
            id="link-title"
            type="text"
            v-model="linkTitle"
            placeholder="鼠标悬停时显示的标题"
            class="md-dialog__input"
          />
        </div>
      </div>
      <div class="md-dialog__footer">
        <button class="md-dialog__btn md-dialog__btn--cancel" @click="close">取消</button>
        <button
          class="md-dialog__btn md-dialog__btn--primary"
          @click="confirm"
          :disabled="!isValid"
        >
          确认
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch, nextTick } from 'vue';

interface Props {
  modelValue: boolean;
  initialData?: {
    text?: string;
    url?: string;
    title?: string;
  };
}

const props = defineProps<Props>();

const emit = defineEmits<{
  (e: 'update:modelValue', value: boolean): void;
  (e: 'confirm', data: { text: string; url: string; title: string }): void;
}>();

const linkText = ref(props.initialData?.text || '');
const linkUrl = ref(props.initialData?.url || '');
const linkTitle = ref(props.initialData?.title || '');
const textInputRef = ref<HTMLInputElement | null>(null);

// 验证表单
const isValid = computed(() => {
  return linkText.value.trim() !== '' && linkUrl.value.trim() !== '';
});

// 关闭对话框
const close = () => {
  emit('update:modelValue', false);
};

// 确认提交
const confirm = () => {
  if (!isValid.value) return;
  
  emit('confirm', {
    text: linkText.value.trim(),
    url: linkUrl.value.trim(),
    title: linkTitle.value.trim()
  });
  
  close();
};

// 监听对话框打开，自动聚焦
watch(
  () => props.modelValue,
  (newVal) => {
    if (newVal) {
      // 重置表单
      linkText.value = props.initialData?.text || '';
      linkUrl.value = props.initialData?.url || '';
      linkTitle.value = props.initialData?.title || '';
      
      // 聚焦第一个输入框
      nextTick(() => {
        textInputRef.value?.focus();
      });
    }
  }
);
</script>

<style lang="scss" scoped>
.md-dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.md-dialog {
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  width: 400px;
  max-width: 90%;
  
  &__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    border-bottom: 1px solid #e8e8e8;
  }
  
  &__title {
    margin: 0;
    font-size: 16px;
    font-weight: 500;
  }
  
  &__close {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    color: #999;
    padding: 0;
    line-height: 1;
    
    &:hover {
      color: #666;
    }
  }
  
  &__body {
    padding: 16px;
  }
  
  &__form-group {
    margin-bottom: 16px;
    
    label {
      display: block;
      margin-bottom: 8px;
      font-size: 14px;
      color: #333;
    }
  }
  
  &__input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid #d9d9d9;
    border-radius: 4px;
    font-size: 14px;
    line-height: 1.5;
    transition: all 0.3s;
    
    &:focus {
      border-color: #1890ff;
      outline: none;
      box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
    }
    
    &::placeholder {
      color: #bfbfbf;
    }
  }
  
  &__footer {
    padding: 10px 16px;
    text-align: right;
    background: #f7f7f7;
    border-top: 1px solid #e8e8e8;
    border-radius: 0 0 4px 4px;
  }
  
  &__btn {
    padding: 6px 16px;
    font-size: 14px;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s;
    border: 1px solid transparent;
    
    &--cancel {
      background-color: #fff;
      border-color: #d9d9d9;
      color: rgba(0, 0, 0, 0.65);
      margin-right: 8px;
      
      &:hover {
        color: #40a9ff;
        border-color: #40a9ff;
      }
    }
    
    &--primary {
      background-color: #1890ff;
      color: #fff;
      
      &:hover {
        background-color: #40a9ff;
      }
      
      &:disabled {
        background-color: #f5f5f5;
        border-color: #d9d9d9;
        color: rgba(0, 0, 0, 0.25);
        cursor: not-allowed;
      }
    }
  }
}
</style>